﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Commlibrary;

namespace StockAssistant
{
    public partial class frmAddStock : Form
    {
        public String sStockNo = string.Empty;
        public String sStockName = string.Empty;
        public String sBuyNum = string.Empty;
        public String sPrice = string.Empty;
        public String sBuyTime = string.Empty;
        private StockHelp stockHelp = new StockHelp();
        public DS_Stock m_dsStock;
        public frmAddStock()
        {
            InitializeComponent();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            this.sStockNo = this.txtStockNo.Text.Trim();
            this.sBuyNum = this.txtBuyNum.Text.Trim();
            this.sPrice = this.txtPrice.Text.Trim();
            this.sBuyTime = this.txtBuyTime.Text.Trim();
            this.sStockName = this.txtStockName.Text.Trim();

            //test
            if (sStockNo.Length == 0)
            {
                MessageBox.Show("Must input StockNo!");
                this.txtStockNo.Focus();
                return;
            }
            if (!Commlibrary.CommonCheck.isInteger(sStockNo))
            {
                MessageBox.Show("Must input integer!");
                this.txtStockNo.Focus();
                return;
            }
            if (sStockNo.Length != 6)
            {
                MessageBox.Show("Must input 6digit number!");
                this.txtStockNo.Focus();
                return;
            }

            if (sBuyNum.Length == 0)
            {
                MessageBox.Show("Must input BuyNum!");
                this.txtBuyNum.Focus();
                return;
            }
            if (!Commlibrary.CommonCheck.isInteger(sBuyNum))
            {
                MessageBox.Show("Must input integer!");
                this.txtBuyNum.Focus();
                return;
            }

            if (sPrice.Length == 0)
            {
                MessageBox.Show("Must input Price!");
                this.txtPrice.Focus();
                return;
            }
            if (!Commlibrary.CommonCheck.isFloat(sPrice))
            {
                MessageBox.Show("Must input float!");
                this.txtPrice.Focus();
                return;
            }

            if (m_dsStock != null && m_dsStock.T_USER_STOCK.Rows.Count > 0)
            {
                if (m_dsStock.T_USER_STOCK.Select("STOCK_NO = '" + sStockNo + "'").Length > 0)
                {
                    MessageBox.Show("Same stock already exist!");
                    this.txtStockNo.Focus();
                    return;
                }
            }

            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void frmAddStock_Load(object sender, EventArgs e)
        {
            this.txtBuyTime.Text = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
        }

        private void txtStockNo_Validating(object sender, CancelEventArgs e)
        {
            String sStockTemp = this.txtStockNo.Text.Trim();
            if (sStockTemp.Length > 0 &&
                !this.sStockNo.Equals(sStockTemp))
            {
                this.sStockNo = sStockTemp;
                String[] info = this.stockHelp.getStockInfoByCode(this.sStockNo);
                if (info == null)
                {
                    e.Cancel = true;
                    this.txtStockName.Text = string.Empty;
                    return;
                }

                if (info.Length > 0 && info[3].Length == 0)
                {
                    e.Cancel = true;
                    this.txtStockName.Text = string.Empty;
                    return;
                }

                String sStockNo = info[0];
                String sStockName = info[1];
                this.txtStockName.Text = info[1];

                String sStockTime = info[2];
                // 现价
                String sCurrPrice = info[3];
                this.txtPrice.Text = sCurrPrice;
                String sYestedayEndPrice = info[4];
                String sTodayBeginPrice = info[5];
                // 涨幅1
                String sAddedPrice = info[6];
                //
                String sLowerPrice = info[7];
                String sTopPrice = info[8];
                // 涨幅2
                String sAppreciate = info[9];
                // 成交手
                String sPayment = info[10];
            }
        }
    }
}
